package easycloud.ecs.agent.batch;

import java.util.concurrent.LinkedBlockingQueue;

import javax.inject.Inject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import easycloud.ecs.common.service.LogService;
import easycloud.ecs.provider.service.SendLogService;

@Service
public class SendLogJob implements SendLogService{
	private static final Log logger = LogFactory.getLog(SendLogJob.class);

	@Inject
	private LogService logService;

	private static final LinkedBlockingQueue<easycloud.ecs.common.domain.Log> logBuffer = new LinkedBlockingQueue<easycloud.ecs.common.domain.Log>();

	private static final int interval = 10;

	public void addLog(easycloud.ecs.common.domain.Log log) {
		SendLogJob.logBuffer.add(log);
	}

	/**
	 */
	@Scheduled(fixedRate = 10000)
	public void execute() {
		SendLogJob.logger.debug("Execute SendLogJob Begin");

		int size = SendLogJob.logBuffer.size();
		while (size-- > 0) {
			easycloud.ecs.common.domain.Log log = null;
			try {
				log = SendLogJob.logBuffer.take();
			} catch (InterruptedException e) {
			}
			logService.submitLog(log);
		}
		SendLogJob.logger.debug("Execute SendLogJob End");
	}
}
